package com.zxs.service.impl;
import java.math.BigDecimal;

import com.zxs.constant.OrderConstant;
import com.zxs.domain.SimpleOrder;
import com.zxs.domain.dto.PaymentInput;
import com.zxs.domain.dto.PaymentParamsDto;
import com.zxs.domain.model.NotifyModel;
import com.zxs.mapper.SimpleOrderMapper;
import com.zxs.service.KuaiqianPaymentService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class KuaiqianPaymentServiceImpl implements KuaiqianPaymentService {

    @Resource
    private SimpleOrderMapper simpleOrderMapper;

    /**
     * 创建支付参数
     * @param paymentInput
     * @return
     */
    @Override
    public PaymentParamsDto createPaymentDto(PaymentInput paymentInput) {

        //1、 创建参数对象，创建快钱需要的参数和值
        PaymentParamsDto paymentParamsDto = new PaymentParamsDto(paymentInput);

        //2、创建SimpleOrder记录
        SimpleOrder simpleOrder = new SimpleOrder();
        simpleOrder.setUid("DL001");
        simpleOrder.setOrderNo(paymentParamsDto.getOrderId());
        simpleOrder.setStatus(0);
        simpleOrder.setPayMoney(paymentInput.getOrderAmount());
        simpleOrder.setPayTime(new Date());
        simpleOrder.setRemark("创建快钱支付");
        simpleOrder.setGoodsReplId("SP001");

        simpleOrderMapper.insert(simpleOrder);

        return paymentParamsDto;
    }


    /**
     * 处理异步请求
     * @param notifyModel
     * @return
     *
     * 1、查询订单是否存在
     * 2、判断订单状态，是否处理过（订单只能处理一次）
     * 3、判断订单金额是否一致
     * 4、判断支付结果，更新订单状态
     *
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String handleNotify(NotifyModel notifyModel) {

        //1、查询订单是否存在
        SimpleOrder simpleOrder = simpleOrderMapper.selectOrderByOrderNO(notifyModel.getOrderId());
        if (simpleOrder == null) {
            //订单不存在
            return "ORDER_NULL";
        }

        //2、判断订单状态，是否处理过（订单只能处理一次）
        if (simpleOrder.getStatus() != OrderConstant.ORDER_STATUS_CREATE) {
            //说明已经处理过了
            return "ORDER_HANDLED";
        }

        //3、判断订单金额是否一致
        BigDecimal payMoney = simpleOrder.getPayMoney();  //订单金额 元
        //元转分
        BigDecimal fen = payMoney.multiply(BigDecimal.valueOf(100)).stripTrailingZeros();
        //快钱返回的支付金额
        BigDecimal payAmount = new BigDecimal(notifyModel.getPayAmount());
        if (!payAmount.equals(fen)) {
            //金额不一致
            return "ORDER_MONEY_ERROR";
        }

        //4、判断支付结果，更新订单状态  10 支付成功  11 支付失败
        if (notifyModel.getPayResult().equals("10")) {
            //支付成功  更新订单表的状态
            Integer update = simpleOrderMapper.updateOrderStatusByOrderNO(simpleOrder.getOrderNo(), OrderConstant.ORDER_STATUS_SUCCEED);
            if (update == 0) {
                return "ORDER_HANDLED_FAIL";  //更新失败
            }
        }else {
            Integer update = simpleOrderMapper.updateOrderStatusByOrderNO(simpleOrder.getOrderNo(), OrderConstant.ORDER_STATUS_FAIL);
            if (update == 0) {
                return "ORDER_HANDLED_FAIL";  //更新失败
            }
        }

        return "ORDER_HANDLED_FINISH";
    }
}
